<?php
App::uses('AppController', 'Controller');

class MenuManageController extends AppController {
	var $helpers = array('Paginator','Html');
	public $user = "";
	var $components = array('Link','Strings');
	public function beforeFilter() {
		parent::beforeFilter();
		$this->user = parent::checkAdminOrManagerLogin();
	}
	/**
	* hiển thị 
	*/
	public function admin_index() {
		$this -> loadModel('SubMenu');
		$this->paginate = array(
                                'limit' => ITEM_PER_PAGE,
                                'order' => array('id' => 'desc'),
                             );
		$conditions = array();
        $data = $this->paginate("SubMenu",$conditions);
				
        $this->set("data",$data);
		$this->set("title_for_layout","Quản lý Menu");
	}
	
	public function admin_add(){
		$this -> loadModel('MenuCategory');
		$this -> loadModel('SubMenu');
		
		$menuCat = $this->MenuCategory->findNameAndId();
		$this->set("menuCat",$menuCat);
		if ($this -> request -> is('post')) {
			$new = $this->request->data;
			$new["short_link"] = $this->Link->getShortLink($new['name']);
			$new['deleted'] = 0;
			$new['created_by'] = $this->user['id'];
			$new['created_at'] = date("Y-m-d H:i:s");
			$new['updated_by'] = $this->user['id'];
			$new['updated_at'] = date("Y-m-d H:i:s");
			$this->SubMenu->save($new);
			$this->Session->setFlash('Thêm danh mục thành công');
			$this->redirect(array('action' => 'index'));
		}
		
		$this->set("title_for_layout","Thêm menu");
	}
	
	public function admin_edit($id){
		$this -> loadModel('MenuCategory');
		$this -> loadModel('SubMenu');
		
		$menuCat = $this->MenuCategory->findNameAndId();
		$this->set("menuCat",$menuCat);
		
		$conditions = array("SubMenu.id" => $id);
		
		$data = $this->SubMenu->find('first', array('conditions' => $conditions));
		if(empty($data)){
			$this->redirect(array('action' => 'index'));
		}
		$this->set("data",$data['SubMenu']);
		if ($this -> request -> is('post')) {
			$new = $this->request->data;
			$new['updated_by'] = $this->user['id'];
			$new['updated_at'] = date("Y-m-d H:i:s");
			$this->SubMenu->save($new);
			$this->Session->setFlash('Chỉnh sửa thành công');
			$this->redirect(array('action' => 'index'));
		}
		
		$this->set("title_for_layout","Sửa menu");
	}
	
	public function admin_delete($id){
		$this -> loadModel('SubMenu');
		$conditions = array("SubMenu.id" => $id);
		$data = $this->SubMenu->find('first', array('conditions' => $conditions));
		if(!empty($data)){
			$update['deleted'] = 1;
			$update['id'] = $id;
			$update['updated_by'] = $this->user['id'];
			$update['updated_at'] = date("Y-m-d H:i:s");
			$this->SubMenu->save($update);
		}
		$this->redirect(array('action' => 'index'));
	}

}
?>